package com.interview.checkinfo.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.interview.checkinfo.entity.CheckInfo;
import com.interview.checkinfo.query.CheckInfoConditionQuery;
import com.interview.checkinfo.query.Page;
import com.interview.checkinfo.service.CheckInfoService;
import com.interview.core.attachment.entity.Attachment;
import com.interview.core.attachment.service.AttachmentService;
import com.interview.employeeInfo.entity.EmployeeInfo;
import com.interview.employeeInfo.service.EmployeeInfoService;

@Controller
@RequestMapping(value = "checkinfo")
public class CheckInfoController {
	@Resource
	private CheckInfoService checkInfoService;
	@Resource
	private EmployeeInfoService employeeInfoService;

	@Resource
	private AttachmentService attachmentService;

	@RequestMapping(value = "page", method = { RequestMethod.GET })
	public String page(ModelMap model,
			CheckInfoConditionQuery checkInfoConditionQuery) {

		Page page = checkInfoService.dynamicQuery(checkInfoConditionQuery);

		Integer total = page.getTotal();
		Integer pageIndex = checkInfoConditionQuery.getPageIndex();
		Integer pageSize = 10;

		Integer totalPage = (total / pageSize);

		if (total % pageSize != 0) {
			totalPage = totalPage + 1;
		}

		if (totalPage <= 0) {
			totalPage = 1;

		}
		
		if (pageIndex >= totalPage & totalPage != 0) {
			pageIndex = totalPage - 1;
		}

		List<CheckInfo> checkInfoes = page.getList();

		model.put("pageIndex", pageIndex);
		model.put("pageSize", pageSize);
		model.put("totalPage", totalPage);
		model.put("list", checkInfoes);
		model.put("conditionQuery", checkInfoConditionQuery);

		return "checkinfo/page";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String checkInfoAdd(ModelMap map) {
		return "checkinfo/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String doAdd(CheckInfo checkInfo, String employeeInfoid, ModelMap map) {

		EmployeeInfo employeeInfo = employeeInfoService.findOne(employeeInfoid);
		checkInfo.setEmployeeInfo(employeeInfo);
		checkInfoService.save(checkInfo);
		return "redirect:/checkinfo/page";
	}

	@RequestMapping(value = "{id}/edit", method = RequestMethod.GET)
	public String checkInfoEdit(@PathVariable String id, ModelMap map) {

		CheckInfo checkInfo = checkInfoService.findOne(id);
		map.put("checkInfo", checkInfo);
		return "checkinfo/edit";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public String doEdit(CheckInfo checkInfo, ModelMap map) {
		checkInfoService.save(checkInfo);
		return "redirect:/checkinfo/page";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable String id,
			CheckInfoConditionQuery checkInfoConditionQuery, ModelMap map) {

		checkInfoService.delet(id);

		return "redirect:/checkinfo/page";
	}

	@RequestMapping(value = "{id}/view", method = RequestMethod.GET)
	public String view(@PathVariable String id, ModelMap model) {
		String relativeFilePath = null;
		CheckInfo checkInfo = checkInfoService.findOne(id);

		EmployeeInfo employeeInfo = checkInfo.getEmployeeInfo();
		String attachmentId = employeeInfo.getAttachmentId();

		model.put("attachmentId", attachmentId);
		model.put("checkInfo", checkInfo);
		return "checkinfo/view";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));// true:允许输入空值，false:不能为空值
	}

	// json格式数据-----------------------------------------------------

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public Object checkInfoData(@PathVariable String id, ModelMap model) {
		return checkInfoService.findOne(id);
	}

	@RequestMapping()
	@ResponseBody
	public Object checkInfoAllData(
			CheckInfoConditionQuery checkInfoConditionQuery) {
		return checkInfoService.dynamicQuery(checkInfoConditionQuery);
	}

	// 外部插件--------------------------------------------------
	@RequestMapping(value = "datecontrol", method = RequestMethod.GET)
	public String showDateControls(ModelMap map) {
		return "checkinfo/datecontrol";
	}
}
